package com.symria.server.dao.role;

import java.util.List;

import com.symria.framework.dao.util.TemplateDao;
import com.symria.framework.page.Page;
import com.symria.server.po.AuthInfo;
import com.symria.server.po.Constant;
import com.symria.server.po.MenuInfo;
import com.symria.server.po.RoleInfo;
import com.symria.system.statement.Statement;

public class RoleDao extends TemplateDao {

	public List<MenuInfo> queryRootMenu(){
        Statement stms = stmsFactory.createStatement("from MenuInfo m  where m.PId is null");
                  stms.addOrderBy(" m.menuSeq desc");
        return templateDao.query(stms);
	}
	public List<AuthInfo> queryRootAuth(){
        Statement stms = stmsFactory.createStatement("from AuthInfo au where au.pid is null");
                  stms.addOrderBy(" au.id desc");
        return templateDao.query(stms);
	}
	public List<RoleInfo> queryRoleInfo(){
        Statement stms = stmsFactory.createStatement("from RoleInfo r where 1=1");
                  stms.addOrderBy(" r.id desc");
        return templateDao.query(stms);
	}
	public List<MenuInfo> queryMenuInfoByRoleId(String visitility,Integer roleId){
		//Statement stms = stmsFactory.createStatement("select from MenuInfo where id in (select pm.menuId from RoleMenu pm where pm.roleId=?)");
		Statement stms = stmsFactory.createStatement("select m from MenuInfo m ,RoleMenu rm where m.id=rm.menuId and rm.roleId=?");
		stms.addParam(roleId);
		stms.append("and", "m.visitility", "=", visitility);
        stms.addOrderBy(" m.menuSeq desc");
        return templateDao.query(stms);
	}
	
	public void deleteRoleMenuByRoleId(Integer roleId){
		Statement stms = stmsFactory.createNativeStatement("DELETE rm FROM ROLE_MENU rm WHERE rm.ROLE_ID=?");
        stms.addParam(roleId);
        templateDao.update(stms);
	}
	public List<AuthInfo> queryAuthInfoByRoleId(Integer roleId){
		Statement stms = stmsFactory.createStatement("from AuthInfo where id in (select pm.authId from RoleAuth pm where pm.roleId=?)");
		stms.addParam(roleId);
		return templateDao.query(stms);
	}
	public void deleteRoleAuthByRoleId(Integer roleId){
		Statement stms = stmsFactory.createNativeStatement("DELETE ra FROM ROLE_AUTH ra WHERE ra.ROLE_ID=?");
        stms.addParam(roleId);
        templateDao.update(stms);
	}
	public Constant getMaxCode(String pcode) {
		Statement stms = this.stmsFactory.createStatement("from Constant where code=(select max(code) from Constant where pcode=?)");
		stms.addParam(pcode);
	   return templateDao.find(stms,Constant.class);
	}
	public void searchConstants(String name,String pcode,String display,Page page){
		Statement stms = stmsFactory.createStatement("from Constant where 1=1");
		          stms.append("and", "pcode", "=", pcode);
		          stms.append("and", "display", "=", display);
		          stms.appendLike(null,"and", "contsName", name);
		          stms.addOrderBy("sort desc");
		          templateDao.search(stms, page);
	}
	public List<Constant> queryConstantsByPcode(String pcode,String display){
		Statement stms = stmsFactory.createStatement("from Constant where pcode=?");
		          stms.addParam(pcode);
		          stms.append("and", "display", "=",display);
		return templateDao.query(stms);
	}
	public Constant findConstantByCode(String code){
		Statement stms = stmsFactory.createStatement("from Constant where code=?");
		          stms.addParam(code);
		return templateDao.find(stms,Constant.class);          
	}
}
